package com.dianxun.exam.net;

import android.content.Context;

import com.dianxun.exam.R;
import com.dianxun.exam.common.AppConfig;
import com.dianxun.exam.utils.ToastUtil;
import com.yanzhenjie.nohttp.Headers;
import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.RequestMethod;
import com.yanzhenjie.nohttp.download.DownloadListener;
import com.yanzhenjie.nohttp.download.DownloadRequest;
import com.yanzhenjie.nohttp.error.NetworkError;
import com.yanzhenjie.nohttp.error.ServerError;
import com.yanzhenjie.nohttp.error.StorageReadWriteError;
import com.yanzhenjie.nohttp.error.StorageSpaceNotEnoughError;
import com.yanzhenjie.nohttp.error.TimeoutError;
import com.yanzhenjie.nohttp.error.URLError;
import com.yanzhenjie.nohttp.error.UnKnownHostError;

/**
 * 下载管理类
 */
public class DownloadServer {

    /**
     * 单个文件下载监听的标志。
     */
    private final int WHAT_DOWNLOAD_SINGLE = 0xA2;

    /**
     * 下载请求.
     */
    private DownloadRequest mDownloadRequest;

    private static DownloadServer instance;

    public static DownloadServer getInstance() {
        if (instance == null)
            synchronized (DownloadServer.class) {
                if (instance == null)
                    instance = new DownloadServer();
            }
        return instance;
    }

    /**
     * 开始下载。
     */
    public void download(Context context, String url, final HttpListener<String> callBack, final DownListener downListener) {
        // 开始下载了，但是任务没有完成，代表正在下载，那么暂停下载。
        if (mDownloadRequest != null) {
            // 暂停下载。
            mDownloadRequest.cancel();
            mDownloadRequest = null;
        }
        mDownloadRequest = new DownloadRequest(url, RequestMethod.GET,
                AppConfig.getInstance().APP_PATH_ROOT,
                true, true);

        CallServer.getInstance().download(WHAT_DOWNLOAD_SINGLE, mDownloadRequest, new DownloadListener() {

            @Override
            public void onStart(int what, boolean isResume, long beforeLength, Headers headers, long allCount) {

            }

            @Override
            public void onDownloadError(int what, Exception exception) {
                Logger.e(exception);

                int message;
                if (exception instanceof ServerError) {
                    message = R.string.download_error_server;
                } else if (exception instanceof NetworkError) {
                    message = R.string.download_error_network;
                } else if (exception instanceof StorageReadWriteError) {
                    message = R.string.download_error_storage;
                } else if (exception instanceof StorageSpaceNotEnoughError) {
                    message = R.string.download_error_space;
                } else if (exception instanceof TimeoutError) {
                    message = R.string.download_error_timeout;
                } else if (exception instanceof UnKnownHostError) {
                    message = R.string.download_error_un_know_host;
                } else if (exception instanceof URLError) {
                    message = R.string.download_error_url;
                } else {
                    message = R.string.error_unknow;
                }
                ToastUtil.toast(message);
                callBack.onFailed();
            }

            @Override
            public void onProgress(int what, int progress, long fileCount, long speed) {
                downListener.onProgress(progress, fileCount, speed);
            }

            @Override
            public void onFinish(int what, String filePath) {
                callBack.onSucceed(filePath);
            }

            @Override
            public void onCancel(int what) {
//                    ToastUtil.toast(R.string.download_status_cancel);
                callBack.onFailed();
            }
        });
    }

    public void cancelRequest() {
        mDownloadRequest.cancel();
        mDownloadRequest = null;
    }
}
